package com.yyh.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyh.entity.Roles;
import com.yyh.entity.UsersParam;
import com.yyh.service.IRolesService;
import com.yyh.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangyueheng
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/roles")
public class RolesController {
	@Autowired
	IRolesService service;
	
	@RequestMapping("/getList")
	public Object getList(UsersParam p) {
		//分页-需要分页的配置文件
		Page<Roles> page = new Page<>(p.getPage(),p.getLimit());
		
		//多条件
		QueryWrapper<Roles> queryWrapper = new QueryWrapper<Roles>();
		IPage<Roles> ipage = service.page(page, queryWrapper);
		
		List<Roles> data = ipage.getRecords();
		long total = ipage.getTotal();
		return new Result(0,"",data,total);
	}
	@RequestMapping("/getList1")
	public Object getList1(UsersParam p) {
		  List<Roles> list = service.list();
		return list;
		
	}
	@PostMapping("/insert")
	public Object insert(@Valid Roles roles) {
		boolean res = service.save(roles);
		if(res) {
			return new Result(0,"添加成功");
		}else {
			return new Result(1,"添加失败");
		}
	}
	
	@PostMapping("/update")
	public Object update(@Valid Roles roles,@RequestParam Integer id) {
		roles.setId(id);
		boolean res = service.updateById(roles);
		if(res) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败");
		}
	}
	@PostMapping("/delete")
	public Object delete(@RequestParam Integer id) {
		boolean res = service.removeById(id);
		if(res) {
			return new Result(0,"删除成功");
		}else {
			return new Result(1,"删除失败");
		}
	}
	
}